package com.maxd.service;

import com.google.common.collect.Lists;
import com.maxd.adapter.LoginUserHolder;
import com.maxd.bean.PageRes;
import com.maxd.error.ExceptionEnum;
import com.maxd.model.OrderPartnerInfo;
import com.maxd.model.StudentAppraisalRec;
import com.maxd.respository.StudentAppraisalRecRepository;
import com.maxd.service.inf.IStudentAppraisalService;
import com.maxd.service.partner.IPartnerService;
import com.maxd.utils.AssertUtil;
import com.maxd.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.persistence.criteria.Predicate;
import java.util.Date;
import java.util.List;

@Service
public class StudentAppraisalService implements IStudentAppraisalService {

    @Autowired
    private StudentAppraisalRecRepository studentAppraisalRecRepository;

    @Autowired
    private IPartnerService partnerService;

    @Override
    public void save(StudentAppraisalRec studentAppraisalRec) {
        Long userId = LoginUserHolder.getUserId();
        AssertUtil.assertTrue(userId != null && userId > 0, ExceptionEnum.WRONT_TOKEN);
        // 校验必须是合伙人才能操作
        List<OrderPartnerInfo> partnerInfos = partnerService.queryUserPartnerList(userId, null);
        AssertUtil.assertTrue(!CollectionUtils.isEmpty(partnerInfos), ExceptionEnum.PRIVILEGE_ERROR);

        if(studentAppraisalRec.getId() != null) {
            studentAppraisalRec.setUpdateTime(new Date());
            studentAppraisalRec.setUpdateBy(String.valueOf(LoginUserHolder.getUserId()));
        } else {
            studentAppraisalRec.setCreateBy(String.valueOf(LoginUserHolder.getUserId()));
            studentAppraisalRec.setUpdateBy(String.valueOf(LoginUserHolder.getUserId()));
        }
        studentAppraisalRecRepository.save(studentAppraisalRec);
    }

    @Override
    public PageRes<StudentAppraisalRec> queryPage(Integer pageNo, Integer pageSize, Long studentId) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.by(order));
        Specification<StudentAppraisalRec> condition = buildCondition(studentId);
        Page<StudentAppraisalRec> pageList = studentAppraisalRecRepository.findAll(condition, pageable);
        return PageUtils.toPageRes(pageList);
    }

    @Override
    public List<StudentAppraisalRec> queryList(Long studentId) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Specification<StudentAppraisalRec> condition = buildCondition(studentId);
        return studentAppraisalRecRepository.findAll(condition, sort);
    }

    private Specification<StudentAppraisalRec> buildCondition(Long studentId) {
        Specification<StudentAppraisalRec> queryCondition = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = Lists.newArrayList();
            if(studentId != null) {
                predicates.add(criteriaBuilder.equal(root.get("studentId"), studentId));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        return queryCondition;
    }
}
